package net.jgrm.common.util;

import net.jgrm.common.exception.BaseExceptionEnum;
import net.jgrm.common.model.Result;

/**
 * 统一返回结果工具类
 *
 * @author service-common
 */
public class ResultUtils {

    // ==================== 响应码定义 ====================

    /**
     * 成功
     */
    public static final int SUCCESS_CODE = 200;

    /**
     * 服务器异常
     */
    public static final int SERVICE_ERROR_CODE = 500;

    /**
     * 请求头无token错误
     */
    public static final int HEADER_UN_LOGIN_ERROR = 600;

    /**
     * 请求头token验证失败
     */
    public static final int HEADER_UN_CREDIBLE_TOKEN_ERROR = 601;

    /**
     * 订单状态不匹配
     */
    public static final int SERVICE_ORDER_STATUS_ERROR = 501;

    /**
     * 余额不足
     */
    public static final int INSUFFICIENT_BALANCE_CODE = 502;

    /**
     * 未设置支付密码
     */
    public static final int UNSET_PAYPASS_CODE = 503;

    /**
     * 支付密码输入错误
     */
    public static final int PAYPASS_ERROR_CODE = 504;

    // ==================== 响应消息定义 ====================

    public static final String SUCCESS_MSG = "成功";
    public static final String SERVICE_ERROR_MSG = "服务器异常！";

    // ==================== 私有构造函数 ====================

    private ResultUtils() {
        // 工具类不允许实例化
    }

    // ==================== 公共方法 ====================

    /**
     * 根据响应码获取结果
     *
     * @param code 响应码
     * @return Result
     */
    public static Result getResult(int code) {
        String msg = "";
        switch (code) {
            case SUCCESS_CODE:
                msg = SUCCESS_MSG;
                break;
            case SERVICE_ERROR_CODE:
                msg = SERVICE_ERROR_MSG;
                break;
            case SERVICE_ORDER_STATUS_ERROR:
                msg = "订单状态不匹配！";
                break;
            case INSUFFICIENT_BALANCE_CODE:
                msg = "余额不足！";
                break;
            case UNSET_PAYPASS_CODE:
                msg = "请设置支付密码！";
                break;
            case PAYPASS_ERROR_CODE:
                msg = "支付密码错误！";
                break;
            default:
                msg = "未知错误！";
                break;
        }
        return new Result(code, msg);
    }

    /**
     * 根据响应码和消息获取结果
     *
     * @param code 响应码
     * @param msg  响应消息
     * @return Result
     */
    public static Result getResult(Integer code, String msg) {
        return new Result(code, msg);
    }

    /**
     * 获取成功结果
     *
     * @return Result
     */
    public static Result getSuccessResult() {
        return getResult(SUCCESS_CODE);
    }

    /**
     * 获取成功结果(带数据)
     *
     * @param data 数据
     * @return Result
     */
    public static Result getSuccessResult(Object data) {
        Result result = getResult(SUCCESS_CODE);
        result.setData(data);
        return result;
    }

    /**
     * 获取错误结果
     *
     * @return Result
     */
    public static Result getErrorResult() {
        return getResult(SERVICE_ERROR_CODE);
    }

    /**
     * 获取错误结果(带消息)
     *
     * @param msg 错误消息
     * @return Result
     */
    public static Result getErrorResult(String msg) {
        return getResult(SERVICE_ERROR_CODE, msg);
    }

    /**
     * 获取错误结果(基于异常枚举)
     *
     * @param baseExceptionEnum 异常枚举
     * @return Result
     */
    public static Result getErrorResult(BaseExceptionEnum baseExceptionEnum) {
        return getResult(baseExceptionEnum.getCode(), baseExceptionEnum.getMessage());
    }

    /**
     * 获取错误结果(带数据)
     *
     * @param msg  错误消息
     * @param data 数据
     * @return Result
     */
    public static Result getErrorResult(String msg, Object data) {
        Result result = getResult(SERVICE_ERROR_CODE, msg);
        result.setData(data);
        return result;
    }


}